import * as Cesium from 'cesium';
/**
 * cesium 中 viewer.entities 加入集合， 因此二次封装一个类去管理所有的实体
 */
export default class GroupEntityCollection {
    constructor(viewer) {
        this.viewer = viewer;
        this.list = []; // 存入每次绘制的集合
        this.groupIdList = []; // 记录gourpId 的顺序
    }
    /**
     * 新增一系列实体，每次新增都是一个集合，通过id 去管理集合
     * @param {*} entityList
     * @param {*} groupId
     */
    add(entity, groupId) {
        if (!groupId) {
            throw new Error(' add 方法必须传入 groupId');
        }
        // 记录绘制id
        if (!this.groupIdList.includes(groupId)) {
            this.groupIdList.push(groupId);
        }
        const entityCollection = this.list.find(
            entityCollection => entityCollection.owner.groupId === groupId
        );
        if (entityCollection) {
            entityCollection.add(entity);
            this.viewer.entities.add(entity);
        } else {
            const entityCollection = new Cesium.EntityCollection({
                groupId: groupId, // 组ID
            });
            entityCollection.add(entity);
            this.viewer.entities.add(entity);
            this.list.push(entityCollection);
        }
    }
    /**
     * 通过id 删除实体集合
     * @param {* } groupId
     */
    remove(groupId) {
        if (!groupId) throw new Error('remove 方法必须传入 groupId');
        const removeEntityCollection = this.list.find(
            entityCollection => entityCollection.owner.groupId === groupId
        );
        if (removeEntityCollection) {
            // 删除集合所有实体
            removeEntityCollection.values.forEach(entity => {
                this.viewer.entities.remove(entity);
            });
            // 删除list 记录
            this.list = this.list.filter(
                entityCollection => entityCollection.owner.groupId !== groupId
            );
        }
    }
    /**
     * 清空全部集合
     */
    removeAll() {
        // 删除实体
        this.list.forEach(entityCollection => {
            entityCollection.values.forEach(entity => {
                this.viewer.entities.remove(entity);
            });
        });
        // 删除list
        this.list = [];
    }
    // 获取所有实体
    getAllEntities() {
        const entities = [];
        this.list.forEach(entityCollection => {
            entityCollection.values.forEach(entity => {
                entities.push(entity);
            });
        });
        return entities;
    }
    // 根据groupId 获取entity
    getEntitiesByGroupId(groupId) {
        if (!groupId) throw new Error('getEntitiesByGroupId 方法必须传入 groupId');
        const entities = [];
        const groupEntityCollection = this.list.find(
            entityCollection => entityCollection.owner.groupId === groupId
        );
        // console.log('this.list',this.list)
        // console.log('groupEntityCollection',groupEntityCollection)
        if (groupEntityCollection) {
            groupEntityCollection.values.forEach(entity => {
                entities.push(entity);
            });
        }
        return entities;
    }
}
